package com.guli.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.coupon.dto.PublishCouponDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.coupon.entity.CouponEntity;
import com.guli.coupon.service.CouponService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 优惠券信息
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-11-21 15:59:18
 */
@RestController
@RequestMapping("coupon/coupon")
@Api(tags = "优惠卷类")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @GetMapping("/infos/{ids}")
    public R getCouponsByIds(@PathVariable List<Long> ids){
        List<CouponEntity> couponEntities = couponService.listByIds(ids);
        return R.ok().put("data",couponEntities);
    }

    @GetMapping("/member")
    @ApiOperation(value = "获取当前用户优惠卷领取列表记录")
    public R getMemberCoupons() {
        R data = couponService.getMemberCoupons();
        return data;
    }

    /**
     * 获取当前用户可领取的优惠劵
     * @return
     */
    @GetMapping("/can/catch")
    @ApiOperation(value = "获取当前用户可领取的优惠劵")
    public R getCouponCatch() {
        R data = couponService.getCouponCatch();
        return data;
    }


    /**
     * 优惠卷发布
     * @param publishCouponDto
     * @return
     */
    @PostMapping("/publish")
    public R publishCoupon(@RequestBody PublishCouponDto publishCouponDto) {
        boolean isPublish = couponService.publishCoupon(publishCouponDto);
        if (isPublish) {
            return R.ok("发布成功");
        }
        return R.error("发布失败");
    }

    /**
     * 领取优惠卷
     * @param couponId
     * @return
     */
    @GetMapping("/catch/{couponId}")
    public R getCoupon(@PathVariable("couponId") Long couponId) {
        R isCatch = couponService.getCoupon(couponId);
        return isCatch;
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = couponService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CouponEntity coupon = couponService.getById(id);
        return R.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CouponEntity coupon) {
        couponService.save(coupon);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CouponEntity coupon) {
        couponService.updateById(coupon);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        couponService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
